package com.gw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gw.context.BaseContext;
import com.gw.mapper.PostMapper;
import com.gw.mapper.UserMapper;
import com.gw.pojo.dto.UserUpdateDTO;
import com.gw.pojo.entity.Post;
import com.gw.pojo.entity.User;
import com.gw.pojo.vo.PostSAVO;
import com.gw.pojo.vo.PostVO;
import com.gw.pojo.vo.UserVO;
import com.gw.result.PageResult;
import com.gw.service.PersonalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.beans.Transient;
import java.util.Objects;

@Service
public class PersonalServiceImpl implements PersonalService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PostMapper postMapper;

    /**
     * 修改用户个人信息
     * @param userUpdateDTO
     */
    public void updatePersonal(UserUpdateDTO userUpdateDTO) {
        //TODO 上传头像时调用lyz统一接口，直接放url
        //如果修改密码，将密码加密后存入数据库
        if (!Objects.equals(userUpdateDTO.getPassword(), "")){
            userUpdateDTO.setPassword(DigestUtils.md5DigestAsHex(userUpdateDTO.getPassword().getBytes()));
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        //TODO 暂时不放入id user.setId(BaseContext.getCurrentUserId());
        user.setId(1L);
        userMapper.updateUserById(user);
    }


    /**
     * 查询用户信息
     * @return
     */
    public UserVO getUserDetail() {
        //TODO User user = userMapper.getUserById(BaseContext.getCurrentUserId());
        User user = userMapper.getUserById(1L);
        return UserVO.builder()
                .avatar(user.getAvatar())
                .nickname(user.getNickname())
                .email(user.getEmail())
                .name(user.getName())
                .sex(user.getSex())
                .phone(user.getPhone())
                .grades(user.getGrades())
                .studentId(user.getStudentId())
                .build();
    }


    /**
     * 分页查询我发布的帖子
     * @param page
     * @param size
     * @return
     */
    public PageResult pageMyPost(int page, int size) {
        PageHelper.startPage(page, size);

        Page<PostVO> postVOPage = postMapper.pagePostById(page, size, BaseContext.getCurrentUserId());

        long total = postVOPage.getTotal();
        return new PageResult(total, postVOPage.getResult());

    }

    /**
     * 删除我发布的帖子,并进行归属校验
     * @param postId
     * @param userId
     * @return
     */

    // 注解启用事务管理
    @Override
    @Transactional
    public int deleteMyPost(Long postId,Long userId){
        // 校验是否为本人删除
        Post post=postMapper.getPostById(postId);
        int result=0; //受删除影响的行数，0表示没有删除成功
        if(Objects.equals(post.getHostId(), userId)){
            postMapper.deleteAllReplyByPostId(postId);
            result=postMapper.deletePostById(postId);
        }else {
            throw new RuntimeException("你没有权限删除该帖子,这不是你的帖子");
        }
        return result;
    }

    //TODO 有新评论时，通知贴主  用websocket实现
}
